import { container, singleton } from "tsyringe";
import { RobotAppPage } from "../../page";
import { Task } from "../../../autox/task";
import { Context } from "../../context";
import { Thread } from "threads";
import { $, formatMinute } from "../../../utils";

@singleton()
export class N72Activity extends RobotAppPage {
    title: string = `弹框n72a`
    private isRuning: boolean = false;
    private task: Task;
    private ctx: Context;
    constructor() {
        super();
        this.task = container.resolve(Task)
        this.ctx = container.resolve(Context)
    }
    dispose(): void {
        this.out()
    }
    is(packageName: string): boolean {
        return `com.smile.gifmaker` === packageName  || `com.kuaishou.nebula` === packageName;
    }
    accept(activityName: string): boolean {
        return `n27.a` === activityName || `u27.a` === activityName;
    }
    thread: Thread;
    in(): boolean {
        if (this.isRuning) return;
        this.isRuning = true;
        /**
         * 1. 参加活动
         * 2. 检测开奖
         * 3. 广告关闭
         */
        this.stop();
        this.thread = this.task.start(() => {
            const start = new Date().getTime() / 1000;
            while (true) {
                const joinNode = textMatches(/去评论|抢红包|点我抢红包/).visibleToUser(true).findOne(1000)
                const joinText = joinNode?.text()
                if (joinText === '去评论') {
                    $.clickButton(joinNode)
                    sleep(1000)
                    const send = selector().text('发送').visibleToUser(true).findOne()
                    $.clickButton(send)
                }
                else if (joinText === '抢红包') {
                    $.clickButton(joinNode)
                    sleep(1000)
                }
                else if(joinText === '点我抢红包'){
                    $.clickButton(joinNode)
                    sleep(1000)
                    // 限时点红包
                }
                const node = textMatches(/.*?(感谢主播|本次未中奖|本次任务失败|手慢了.红包派完了|继续围观|后开奖)/).visibleToUser(true).findOne(1000);
                const text = node?.text();
                log(`找到的结果为：${text}`)
                if (!text) {
                    // 没找到 原因有2个 一个是没有 一个是超时 那么都继续等待
                    sleep(1000)
                }
                else if (text === '感谢主播') {
                    // 中奖了
                    const countNode = selector().idContains('red_packet_cover_reward_usual_count').depth(3).drawingOrder(2).visibleToUser(true).findOne()
                    const count = countNode.text()
                    this.ctx.updateUi({ currentResult: `${count}快币`, currentStatus: `恭喜您中奖了`, toast: `中了${count}快币` })
                    this.back();
                }
                else if (text === '继续围观') {
                    this.back();
                }
                else if (text.includes('后开奖')) {
                    this.ctx.updateUi({ currentResult: text, currentStatus: `等待开奖`, toast: `等待开奖-已等待${formatMinute(new Date().getTime() / 1000 - start)}` })
                    sleep(1000)
                }
                else if (text === `很遗憾，本次未中奖`) {
                    this.ctx.updateUi({ toast: text, currentStatus: '任务结束', currentResult: '未中奖' })
                    this.back();
                }
                else if (text === `手慢了，红包派完了`) {
                    this.ctx.updateUi({ toast: text, currentStatus: '任务结束', currentResult: '未中奖' })
                    this.back();
                }
                else if (text === `很遗憾，本次任务失败`) {
                    this.ctx.updateUi({ toast: text, currentStatus: '任务结束', currentResult: '未中奖' })
                    this.back();
                }
                else {
                    this.ctx.updateUi({ currentResult: text, currentStatus: `等待开奖`, toast: `等待开奖-已等待${formatMinute(new Date().getTime() / 1000 - start)}` })
                    sleep(1000)
                }
            }
        })
        return true;
    }
    back() {
        this.stop();
        back();
        sleep(500)
    }
    stop() {
        if (this.thread && this.thread.isAlive()) {
            this.thread.interrupt()
        }
    }
    out(): boolean {
        if (!this.isRuning) return false;
        this.isRuning = false;
        log(`N72Activity out`)
        return true;
    }
}
